package com.huawei.petSystemdemo.ui.my;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.huawei.petSystemdemo.GlobalViewModel;
import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.data.UserRepository;

import java.util.function.Consumer;

public class VipAddActivity extends AppCompatActivity {
    private LinearLayout vip_one;
    private LinearLayout vip_second;
    private LinearLayout vip_third;
    private LinearLayout vip_four;
    private GlobalViewModel global = GlobalViewModel.getGlobal();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip_add);

        vip_one = findViewById(R.id.vip_one);
        vip_second = findViewById(R.id.vip_second);
        vip_third = findViewById(R.id.vip_third);
        vip_four = findViewById(R.id.vip_four);

        vip_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeSure("开通一个月大会员", 15, 30);
            }
        });

        vip_second.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeSure("开通2个月大会员", 28, 60);
            }
        });

        vip_third.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeSure("开通半年大会员", 78, 182);
            }
        });

        vip_four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeSure("开通一年大会员", 120, 365);
            }
        });
    }

    // 带“是”和“否”的提示框
    private void makeSure(String mes, double money, int time){
        new AlertDialog.Builder(this)
                .setTitle("")
                .setMessage(mes)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        UserRepository.updateIntegral(global.user.getUsername(), money, new Consumer<String>() {
                            @Override
                            public void accept(String s) {
                                System.out.println(s);
                            }
                        });
                        UserRepository.saveVip(global.user.getId(), time, new Consumer<String>() {
                            @Override
                            public void accept(String s) {
                                System.out.println(s);
                                Toast.makeText(getBaseContext(), "成功开通一个月大会员", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(VipAddActivity.this, VipActivity.class);
                                startActivity(intent);
                            }
                        });
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }
}